﻿#define ROOTSIGNATURE "\
DescriptorTable(CBV(offset = 0, b0, space = 0, numdescriptors = 2)),\
DescriptorTable(SRV(offset = 0, t0, space = 0, numdescriptors = 6)),\
DescriptorTable(UAV(offset = 0, u0, space = 0, numdescriptors = 4))"

cbuffer g_CBuffer_Mesh:register(b0, space0)
{
	column_major float4x4 g_Mesh_InverseJointBind[256];

	//g_Mesh_JointAnimate * g_Mesh_InverseJointBind * Position //坐标变换

	//Dual Quaternion
	//column major float2x4 g_Mesh_InverseJointBind[256];
};

cbuffer g_CBuffer_Mesh:register(b1, space0)
{
	column_major float4x4 g_Mesh_JointAnimate[256];
};

Buffer<float3> g_BufferVertexPosition : register(t0, space);
Buffer<snorm float3> g_BufferVertexNormal : register(t1, space);
Buffer<snorm float3> g_BufferVertexTangent : register(t2, space);
Buffer<snorm float3> g_BufferVertexBiTangent : register(t3, space);
Buffer<uint4> g_BufferVertexBlendIndex : register(t4, space);
Buffer<unorm float4> g_BufferVertexBlendWeight : register(t5, space);

RWBuffer<float3> g_BufferVertexPosition_Skin : register(u0, space);
RWBuffer<snorm float3> g_BufferVertexNormal_Skin : register(u1, space);
RWBuffer<snorm float3> g_BufferVertexTangent_Skin : register(u2, space);
RWBuffer<snorm float3> g_BufferVertexBiTangent_Skin : register(u3, space);

[RootSignature(ROOTSIGNATURE)]
[numthreads(128, 1, 1)]
void ComputeMain(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{
	uint l_VertexIndex = gl_GlobalInvocationID.x;

	uint4 l_VertexBlendIndex = g_BufferVertexBlendIndex[l_VertexIndex];
	unorm float4 l_VertexBlendWeight = g_BufferVertexBlendWeight[l_VertexIndex];

	float4x4 l_Mesh_Model //ModelSpace
		= mul(g_Mesh_InverseJointBind[l_VertexBlendIndex.x], g_Mesh_JointAnimate[l_VertexBlendIndex.x])*l_VertexBlendWeight.x
		+ mul(g_Mesh_InverseJointBind[l_VertexBlendIndex.y], g_Mesh_JointAnimate[l_VertexBlendIndex.y])*l_VertexBlendWeight.y
		+ mul(g_Mesh_InverseJointBind[l_VertexBlendIndex.z], g_Mesh_JointAnimate[l_VertexBlendIndex.z])*l_VertexBlendWeight.z
		+ mul(g_Mesh_InverseJointBind[l_VertexBlendIndex.w], g_Mesh_JointAnimate[l_VertexBlendIndex.w])*l_VertexBlendWeight.w;

	//--------------------------------------------------------------------------------------------------------
	float4 l_VertexPosition = float4(g_BufferVertexPosition[l_VertexIndex], 1.0f);

	float4 l_VertexPosition_Skin = mul(l_VertexPosition, l_Mesh_Model);//ModelSpace

	g_BufferVertexPosition_Skin[l_VertexIndex] = l_VertexPosition_Skin.xyz;

	//--------------------------------------------------------------------------------------------------------
	unorm float4 l_VertexTangent;
	l_VertexTangent.xyz = g_BufferVertexTangent[l_VertexIndex];
	l_VertexTangent.w = 1.0f;

	unorm float4 l_VertexTangent_Skin = mul(l_VertexTangent, l_Mesh_Model);//ModelSpace
	//l_VertexTangent_Skin.xyzw /= l_VertexTangent_Skin.wwww;

	l_VertexTangent_Skin.xyz = normalize(l_VertexTangent_Skin.xyz);

	g_BufferVertexTangent_Skin[l_VertexIndex] = l_VertexTangent_Skin.xyz;

	//--------------------------------------------------------------------------------------------------------										  
	unorm float4 l_VertexBiTangent;
	l_VertexBiTangent.xyz = g_BufferVertexBiTangent[l_VertexIndex];
	l_VertexBiTangent.w = 1.0f;

	unorm float4 l_VertexBiTangent_Skin = mul(l_VertexBiTangent, l_Mesh_Model);//ModelSpace
	 //l_VertexBiTangent_Skin.xyzw /= l_VertexBiTangent_Skin.wwww;

	l_VertexBiTangent_Skin.xyz = normalize(l_VertexBiTangent_Skin.xyz);

	g_BufferVertexBiTangent_Skin[l_VertexIndex] = l_VertexBiTangent_Skin.xyz;

	//--------------------------------------------------------------------------------------------------------										  
	float3x3 l_Mesh_AdjointModel = float3x3(
		float3(
			l_Mesh_Model._m11*l_Mesh_Model._m22 - l_Mesh_Model._m12*l_Mesh_Model._m21,
			-l_Mesh_Model._m10*l_Mesh_Model._m22 + l_Mesh_Model._m12*l_Mesh_Model._m20,
			l_Mesh_Model._m10*l_Mesh_Model._m21 - l_Mesh_Model._m11*l_Mesh_Model._m20
			),
		float3(
			-l_Mesh_Model._m01*l_Mesh_Model._m22 + l_Mesh_Model._m02*l_Mesh_Model._m21,
			l_Mesh_Model._m00*l_Mesh_Model._m22 - l_Mesh_Model._m02*l_Mesh_Model._m20,
			-l_Mesh_Model._m00*l_Mesh_Model._m21 + l_Mesh_Model._m01*l_Mesh_Model._m20
			),
		float3(
			l_Mesh_Model._m01*l_Mesh_Model._m12 - l_Mesh_Model._m02*l_Mesh_Model._m11,
			-l_Mesh_Model._m00*l_Mesh_Model._m12 + l_Mesh_Model._m02*l_Mesh_Model._m10,
			l_Mesh_Model._m00*l_Mesh_Model._m11 - l_Mesh_Model._m01*l_Mesh_Model._m10
			)
		);

	unorm float3 l_VertexNormal = g_BufferVertexNormal[l_VertexIndex];
	l_VertexNormal = mul(l_Mesh_AdjointModel, l_VertexNormal);//ModelSpace
	l_VertexNormal = normalize(l_VertexNormal);

	g_BufferVertexNormal_Skin[l_VertexIndex] = l_VertexNormal;
}